<?php

namespace plugin\admin\app\controller\user;

use plugin\admin\app\common\xbuilder\XBuilderForm;
use plugin\admin\app\common\xbuilder\XBuilderList;
use plugin\admin\app\controller\Base;
use plugin\admin\app\controller\common\Crud;
use plugin\admin\app\model\UserThink;
use plugin\admin\app\Util;

/**
 * 用户管理
 */
class User extends Base
{
    /**
     * @var User
     */
    protected $model = null;

    /**
     * 增删改查
     */
    use Crud;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new UserThink;
    }

    /**
     * 获取列表数据
     */
    public function index(){
        $sexOpt = [
            'type'=>'mapping',
            'map'=>[
                1=>'男',
                2=>'女',
                0=>'未知'
            ]
        ];
        $builder = new XBuilderList();
        return $builder->setMetaTitle(' ') // 设置页面标题
        ->addTopButton('add',$this->add())
            ->addTopButton('forbid')
            ->addTopButton('recycle')
            ->addTopButton('restore')
            ->addTopButton('delete')
            ->addTableColumn('id', 'ID','',false,true)
            ->addTableColumn('username', '用户名')
            ->addTableColumn('nickname', '昵称')
            ->addTableColumn('sex', '昵称',$sexOpt)
            ->addTableColumn('avatar', '头像','image')
            ->addTableColumn('email', '邮箱')
            ->addTableColumn('mobile', '手机号码')
            ->addTableColumn('level', '等级')
            ->addTableColumn('birthday', '生日','datetime')
            ->addTableColumn('money', '余额')
            ->addTableColumn('score', '积分')
            ->addTableColumn('last_time', '上次登陆时间','datetime')
            ->addTableColumn('last_ip', '上次登陆ip')
            ->addTableColumn('join_time', '注册时间','datetime')
            ->addTableColumn('status', '状态', 'status')
            ->addRightButton('edit',$this->edit()) // 添加编辑弹窗按钮
            ->addRightButton('forbid') // 添加编辑按钮
            ->addRightButton('resume') // 添加编辑按钮
            ->addRightButton('recycle') // 添加编辑按钮
            ->addRightButton('restore') // 添加编辑按钮
            ->addRightButton('delete') // 添加编辑按钮
            ->setTableApi()
            ->getPageData();
    }
    public function add()
    {
        return $this->form('add');
    }
    public function edit()
    {
        return $this->form('edit');
    }
    protected function form($type = 'add')
    {
        if (request()->method() == 'POST') {
            $slider_object = $this->model;
            $data  = request()->except(['create_at']);
            if(isset($data['password']) && $data['password'])
            {
                $data['password'] = Util::passwordHash($data['password']);
            }
            if($type === 'add'){
                $result=$slider_object->save($data);
            } else {
                $result=$slider_object->update($data);
            }
            if ($result) {
                return $this->resSucess('操作成功');
            } else {
                return $this->resError('操作失败');
            }
        } else {
            $statusOpt = [  //单选
                'type'=>'select',
                'options'=>[
                    ['label'=>'未审核', 'value'=>'0'], ['label'=>'驳回', 'value'=>'-1'], ['label'=>'审核通过', 'value'=>'1']
                ]
            ];
            $sexOpt = [  //单选
                'type'=>'select',
                'options'=>[
                    ['label'=>'男', 'value'=>'1'], ['label'=>'女', 'value'=>'2'], ['label'=>'未知', 'value'=>'*']
                ]
            ];
            $emailRule = [
                'required'=>true,
                'rule'=>[
                    'isEmail'=>true
                ],
                'errMsg'=>[
                    'isNumeric'=>'邮箱格式错误',
                ]
            ];
            $telRule = [
                'required'=>true,
                'rule'=>[
                    'matchRegexp'=>'/^[1][3,4,5,7,8,9][0-9]{9}$/'
                ],
                'errMsg'=>[
                    'matchRegexp'=>'请输入正确的手机号码',
                ]
            ];
            $birthdayOpt = [  //单选
                'type'=>'input-date',
                'format'=>'YYYY-MM-DD'
            ];
            $builder = new XBuilderForm();
            return $builder->setMetaTitle('编辑管理员信息')
                ->setFormUrl($type)
                ->setFormMode('horizontal')
                ->addFormItem('username', 'text', '用户名', true)
                ->addFormItem('nickname', 'text', '昵称',true)
                ->addFormItem('password', 'password', '密码')
                ->addFormItem('sex', $sexOpt, '性别')
                ->addFormItem('avatar', 'image','头像')
                ->addFormItem('email', 'text','邮箱',$emailRule)
                ->addFormItem('mobile', 'text','手机号码',$telRule)
                ->addFormItem('birthday', $birthdayOpt,'生日')
                ->addFormItem('status', $statusOpt,'状态')
                ->setFooterAffix()
                ->getFormData();
        }
    }
}
